package mest.controller.cxj;

import com.alibaba.fastjson.JSON;
import mest.Dto.cxj.WorkRouteDto;
import mest.Dto.cxj.WorkrouteWorkManytomanyDto;
import mest.entity.modeling_dictionaries;
import mest.entity.work_route;
import mest.entity.workroutework_manytomany;
import mest.service.cxj.ModelingDictionariesService;
import mest.service.cxj.WorkRouteService;
import mest.service.cxj.WorkrouteWorkManytomanyService;
import mest.service.system.DBService;
import mest.util.NumberConst;
import mest.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("workRoute")
@CrossOrigin
public class WorkRouteController {

    @Autowired
    private WorkRouteService workRouteService;

    @Autowired
    private ModelingDictionariesService modelingDictionariesService;

    @Autowired
    private WorkrouteWorkManytomanyService workrouteWorkManytomanyService;

    @Autowired
    private DBService dbService;


    /**
     * 提供工序档案流水号
     *
     * @return
     */
    @GetMapping("getCode")
    public String getCode() {
        String theirDb=dbService.getCompanyDB();
        modeling_dictionaries modelingdictionaries = modelingDictionariesService.selectModelingDictionaries(theirDb);
        int workRouteNo = modelingdictionaries.getWorkrouteNo();
        return NumberConst.WorkRoute_No + "-" + workRouteNo;
    }


    /**
     * 查询全部工序档案
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("selectAll")
    public String selectAll(@RequestParam Integer page, @RequestParam Integer limit,
                            @RequestParam(required = false) String routeName) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            List<WorkRouteDto> workRouteDtoList = workRouteService.selectAllWorkRouteAndSave(page, limit, routeName,theirDb);
            Integer count = workRouteService.selectCountWorkRoute(routeName,theirDb);
            resultObj = new ResultObj(0, "success", count, workRouteDtoList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }


    /**
     * 根据工艺ID查看相关工序
     *
     * @param wrid
     * @return
     */
    @GetMapping("selectById")
    public String selectById(@RequestParam Integer wrid, @RequestParam(required = false) String workName) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            List<WorkrouteWorkManytomanyDto> workrouteWorkManytomanyDtoList = workrouteWorkManytomanyService.selectWorkRouteAndWorkById(wrid,workName,theirDb);
            if (workrouteWorkManytomanyDtoList.size() != 0) {
                resultObj = new ResultObj(0, "success", workrouteWorkManytomanyDtoList.size(), workrouteWorkManytomanyDtoList);
            } else {
                resultObj = ResultObj.builder().code(-1).msg("没有查到相关工序数据").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }


    /**
     * 添加工艺档案
     *
     * @param workRoute
     * @return
     */
    @PostMapping("insertModel")
    public String insertModel(@RequestBody work_route workRoute) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            if (workRouteService.insertWorkRoute(workRoute,theirDb)) {
                modelingDictionariesService.updateModelingDictionariesByWorkRouteNo(theirDb); //更新工艺档案流水号
                resultObj = ResultObj.builder().code(0).msg("添加成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("添加失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 添加工艺与工序的关系
     *
     * @param workrouteWorkManytomanyList
     * @return
     */
    @PostMapping("insertRelation")
    public String insertRelation(@RequestBody List<workroutework_manytomany> workrouteWorkManytomanyList) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            if (workrouteWorkManytomanyList.size() != 0) {
                if (workrouteWorkManytomanyService.insertWorkRouteWork(workrouteWorkManytomanyList,theirDb)) {
                    resultObj = ResultObj.builder().code(0).msg("添加成功").build();
                }
            } else {
                resultObj = ResultObj.builder().code(-1).msg("添加失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }


    /**
     * 删除工艺档案（假删）
     *
     * @param id
     * @return
     */
    @DeleteMapping("delModel")
    public String delModel(@RequestParam Integer id) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            if (workRouteService.deleteWorkRoute(id,theirDb)) {
                resultObj = ResultObj.builder().code(0).msg("删除成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("删除失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }
}

